package com.dk.service.impl;

import com.dk.mapper.MenuRoleMapper;
import com.dk.mapper.RoleMapper;
import com.dk.pojo.MenuRoleExample;
import com.dk.pojo.Role;
import com.dk.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private MenuRoleMapper menuRoleMapper;

    @Override
    public List<Role> selectInfo() {
        List<Role> roles = roleMapper.selectByExample(null);
        return roles;
    }

    @Override
    public boolean insertinto(Role role, Integer[] mids) {
        try {

            Integer a = roleMapper.insertIntoByrid(role);
            Integer rid = role.getRid();
            Integer w = menuRoleMapper.insertBymidandRid(mids, rid);

            return true;
        } catch (Exception e) {
            e.printStackTrace();
            throw e;

        }

    }

    @Override
    public Role selectByid(Integer rid) {
        Role role = roleMapper.selectByPrimaryKey(rid);
        return role;
    }

    @Override
    public boolean updateInfo(Integer[] mids, Role r) {
        try {
            int i = roleMapper.updateByPrimaryKeySelective(r);
            MenuRoleExample a = new MenuRoleExample();
            MenuRoleExample.Criteria criteria = a.createCriteria();
            criteria.andRidEqualTo(r.getRid());
            menuRoleMapper.deleteByExample(a);
            menuRoleMapper.insertBymidandRid(mids, r.getRid());
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    }

    @Override
    public boolean deleteById(Integer rid) {
        try {


            roleMapper.deleteByPrimaryKey(rid);
            MenuRoleExample a = new MenuRoleExample();
            MenuRoleExample.Criteria criteria = a.createCriteria();
            criteria.andRidEqualTo(rid);
            menuRoleMapper.deleteByExample(a);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    }
}